#include"MyPlayer.h"
int TryGetAudioCodec(FF_AUDIOINFO*audioinfo,AVFormatContext *fmt_ctx){
    audioinfo->idx=FindAudioCodec(fmt_ctx);
    if(audioinfo->idx!=-1){
        AVCodec *dec = NULL;
        audioinfo->stream=fmt_ctx->streams[audioinfo->idx];
        audioinfo->codec=audioinfo->stream->codec;
        dec=avcodec_find_decoder(audioinfo->codec->codec_id);
        if(dec==NULL){
            printf("Find Audio dec failed %s\n",avcodec_get_name(audioinfo->codec->codec_id)); 
            return -1;
        }
       if(avcodec_open2(audioinfo->codec,dec,NULL)<0){
            printf("Open Audio dec failed %s\n",avcodec_get_name(audioinfo->codec->codec_id)); 
            return -1;
        }else{
            g_ff_status.hasaudio=1;
            g_ff_status.sample_rate=audioinfo->codec->sample_rate;
            g_ff_status.samples=audioinfo->codec->frame_size;
            g_ff_status.channels=audioinfo->codec->channels;
#if USE_FFMPEG_SWR            
            audioinfo->pSwr=swr_alloc();
            audioinfo->pSwr=swr_alloc_set_opts(audioinfo->pSwr, 
            AV_CH_LAYOUT_STEREO,AV_SAMPLE_FMT_S16,g_ff_status.sample_rate,
            av_get_default_channel_layout(audioinfo->codec->channels),
            audioinfo->codec->sample_fmt,g_ff_status.sample_rate, 
            0, NULL);
            swr_init(audioinfo->pSwr);
            audioinfo->length = av_samples_get_buffer_size(NULL,av_get_channel_layout_nb_channels(AV_CH_LAYOUT_STEREO),g_ff_status.samples, AV_SAMPLE_FMT_S16,1);
            printf("audioLength:%d size:%d\n",audioinfo->length,audioinfo->codec->frame_size);
#endif            
        }
    }
    return audioinfo->idx;
}

#define MAX_AUDIO_FRAME_SIZE 192000
int16_t floattoint16(float val) {
	int a = val * (1 << 15);
	if ((a + 0x8000U) & ~0xFFFF) {
		return (a >> 31) ^ 0x7FFF;
	}else {
		return a;
	}
}
int TryDecAudioPacket(FF_AUDIOINFO*audioinfo,AVPacket*packet,AVFrame*pFrame){
    double pts=0;
	int frameFinished=0;
    int ret=avcodec_decode_audio4(audioinfo->codec,pFrame,&frameFinished,packet);
	if (ret < 0)return -1;
    if(packet->dts!=AV_NOPTS_VALUE){
       pts=packet->dts;
    }else
	if((packet->dts == AV_NOPTS_VALUE)&&
	   (pFrame->opaque!=NULL)&&
	   (*(uint64_t*)pFrame->opaque != AV_NOPTS_VALUE)){
	   pts=*(int64_t *)pFrame->opaque;	   
	}
	pts*=av_q2d(audioinfo->stream->time_base);
    if(frameFinished){            
       uint8_t*buffer=malloc(MAX_AUDIO_FRAME_SIZE*2);
#if USE_FFMPEG_SWR       
       int  persamples=swr_convert(audioinfo->pSwr,&buffer,48000,(const uint8_t**)pFrame->data,pFrame->nb_samples);
       int  decoded_data_size=av_samples_get_buffer_size(NULL, audioinfo->codec->channels, persamples,AV_SAMPLE_FMT_S16,1);
       //printf("DataSize:%d persamples:%d channels:%d sample:%d\n",decoded_data_size, persamples, audioinfo->codec->channels,pFrame->nb_samples);
	   if(0) {
		   float *ptr = (float*)pFrame->data[0];
		   float *ptr2 = (float*)pFrame->data[1];
		   int16_t*ptr16 = (int16_t*)buffer;
		   int16_t*ptr162 = ptr16 + pFrame->nb_samples;
		   //printf("%0.6f %0.6f %d %d\n", ptr[0], ptr2[0], ptr16[0], ptr162[0]);
		   //printf("%0.6f %0.6f %d %d\n", ptr[1], ptr2[1], ptr16[1], ptr162[1]);
		   printf("%0.6f %0.6f %d %d %d %d\n", ptr[0], ptr2[0], ptr16[0], ptr16[1], (int16_t)(ptr[0]* 32768), (int16_t)(ptr2[0]* 32768));
		   printf("%0.6f %0.6f %d %d %d %d\n", ptr[1], ptr2[1], ptr16[2], ptr16[3], (int16_t)(ptr[1] * 32768), (int16_t)(ptr2[1] * 32768));
	   } 
	   PushAudioPacket(buffer,decoded_data_size,(int)(pts*1000));  
#else
       //int  decoded_data_size=av_samples_get_buffer_size(NULL,2,pFrame->nb_samples,AV_SAMPLE_FMT_FLTP,1); 
       int  decoded_data_size=av_samples_get_buffer_size(NULL,audioinfo->codec->channels,pFrame->nb_samples,AV_SAMPLE_FMT_S16,1);
	   int pos = 0;
	   float *ptr0 = (float*)pFrame->data[0];
	   float *ptr1 = (float*)pFrame->data[1];
	   int16_t*ptr16 = (int16_t*)buffer;
	   //LLLLRRRR=>LRLRLRLR
	   while (pos < pFrame->nb_samples) {
		   //32767 65535
		   //ptr16[pos*2+0] = (int16_t)(ptr0[pos]* 32767);
		   //ptr16[pos*2+1] = (int16_t)(ptr1[pos]* 32767);
		   ptr16[pos * 2 + 0] = floattoint16(ptr0[pos]);
		   ptr16[pos * 2 + 1] = floattoint16(ptr1[pos]);
		   pos++;
	   }
	   //printf("DataSize:%d channels:%d sample:%d %0.6f %0.6f\n",
	   //   decoded_data_size, audioinfo->codec->channels,pFrame->nb_samples, ptr[0],ptr2[0]);
       PushAudioPacket(buffer,decoded_data_size,(int)(pts*1000));
#endif
    }
    return FFMIN(ret, packet->size);
}